'use strict';
const mongoose = require('mongoose');
const md5 = require('md5');
// let schema = new mongoose.Schema({
//
// });
//
// schema.statics = {
//
// };

module.exports={
    async list(){
        let AdminModel=mongoose.models['Admin'];
        let admins=await AdminModel.find({});
        return admins;
    },
    async add(options){
        let AdminModel=mongoose.models['Admin'];
        await AdminModel.create({
            loginId:options.adminLoginId,
            password:md5(options.password)
        })
    },
    async edit(options){
        let AdminModel=mongoose.models['Admin'];
        let admin=await AdminModel.findById(options._id);
        if(!admin){
            throw new Error('这个管理员不存在');
        }
        admin.password=md5(options.password);
        await admin.save();
    },
    async delete(options){
        let AdminModel=mongoose.models['Admin'];
        let admin=await AdminModel.findById(options._id);
        if(!admin){
            throw new Error('这个管理员不存在');
        }
        await AdminModel.deleteOne({_id:options._id});
    },
};

// module.exports = schema;